/*
Title: The Attorney's Gender
Authors: J.S. Hack & C.M. Jenkins
Date create: Feb. 4, 2021
Last modified: 
Dependent file(s): 
	- lawyersStacked.dta (refered herein as: LS) 
	- caseLevel.dta (refered herein as: CL) 
	- justiceLevel.dta (refered herein as: JL)
Description: This do-file contains the commands and codes necessary to 
			 reproduce the analysis conducted in the paper and associated 
			 appendix. Please note: graphs and tables produced running the code
			 in this file may not match the appearance in the paper or appendix.
			 That is the result of using the graph editor or other platforms to
			 create more pleasing visuals. 
*/
*----------------------------------------------------------------------------------------*
*creating the moving wall for repeat appearances
sort name term 
egen lID = group(name) //create lawyer ID based on name
by lID, sort: gen countArguedCases = _n // create a counter 
by lID term, sort: egen movingWall = max(countArguedCases) // find max number of cases 
*----------------------------------------------------------------------------------------*

**Stata environment prolog 
frame create LS 
frame create CL
frame create JL

**Analysis in Paper 
*figure 1 - women over time
frame change LS
by term, sort: egen lawyersCount = count(name) // count of arguing attorneys per term
by term gender, sort: egen womenCount = count(gender) if gender=="female"
by term, sort: egen maxwomenCount = max(womenCount)
gen percentWomen = maxwomenCount / lawyersCount
line percentWomen term, sort 


*table 1 - raw logit for model 1 and 2
frame change CL
logit win ib4.gender_dyad ib3.lawyerstat_p c.movingwall_diff i.appointed_p c.amici_diff ib4.support_pf i.previoussg_p i.clerk_p CSI i.legalSalience scotuslowercongruence term, vce(cluster term) coefleg 
eststo model1

logit win ib4.gender_dyad##c.term c.scotuslowercongruence##i.gender_dyad ib3.lawyerstat_p c.movingwall_diff i.appointed_p c.amici_diff ib4.support_pf i.previoussg_p i.clerk_p CSI i.legalSalience, vce(cluster term) coefleg
eststo model2

esttab model1 model2 using logitCoefs.rtf, replace title("Table 1. Testing Influences on Petitioner Success") mtitle("Model 1" "Model 2") nodepvars nonumbers nogap nobaselevels label varwidth(25)  cells((b(star fmt(2))) se(par)) interaction(" x ") addnotes("Note: Logit coefficients clustered by Supreme Court Term with standard errors in parentheses. *p < 0.05")

*figure 2 - AMEs combined for model 1 and 2
est restore model1 
estpost margins, dydx(*)
coefplot, xline(0, lpattern(-)) omitted  drop(_cons)   headings(1.gender_dyad = "{bf:Gender Effects}" 1.lawyerstat_p = "{bf:Governmental Status}" 1.movingwall_diff = "{bf:Repeat Status}" 1.appointed_p = "{bf:Controls}" 1.support_pf = "{it:Type of Amici Support}")

est restore model2
estpost margins, dydx(*)
coefplot, xline(0, lpattern(-)) omitted  drop(_cons)   headings(1.gender_dyad = "{bf:Gender Effects}" 1.lawyerstat_p = "{bf:Governmental Status}" 1.movingwall_diff = "{bf:Repeat Status}" 1.appointed_p = "{bf:Controls}" 1.support_pf = "{it:Type of Amici Support}")

**NOTE: in the paper these figures are combined using gr combine 

*figure 3 - interaction between gender and term 
est restore model2
margins gender_dyad, at(term = (1946(10)2016))
marginsplot, noci recast(line)

*figure 4 - governmental lawyer success
est restore model1 
margins lawyerstat_p
marginsplot,  recast(scatter) recastci(rspike)

*figure 5 - repeat appearances 
est restore model1 
margins,  at(movingwall_diff=(-76(10)75))
marginsplot, recast(line) recastci(rarea) ytitle("Pr(Petitioner Wins)") 

**The Appendix
frame change LS
*table a.1. - crosstab gender x party 
tab gender party , row

*table a.2 - petitioner success by opposing counsel pairs
frame change CL
tabgender_dyad partyWinning, chi


*figure a.1 - contrast plot dyads x term 
est restore model2
margins ib4.gender_dyad, at(term = (1946(10)2016)) contrast(nowald effects)
marginsplot, recast(line) recastci(rarea) yline(0, lpattern(-)) by(gender_dyad) ytitle("Pr(Petitioner Wins)")

*table a.4 - comparing salience measures 
logit win ib4.gender_dyad ib3.lawyerstat_p c.movingwall_diff i.appointed_p c.amici_diff ib4.support_pf i.previoussg_p i.clerk_p i.nytSalience i.legalSalience scotuslowercongruence term, vce(cluster term)
eststo nyt1

logit win ib4.gender_dyad##c.term  ib4.gender_dyad##c.scotuslowercongruence ib3.lawyerstat_p c.movingwall_diff i.appointed_p c.amici_diff ib4.support_pf i.previoussg_p i.clerk_p i.nytSalience i.legalSalience , vce(cluster term)
eststo nyt2

logit win ib4.gender_dyad ib3.lawyerstat_p c.movingwall_diff i.appointed_p c.amici_diff ib4.support_pf i.previoussg_p i.clerk_p i.cqSalience i.legalSalience scotuslowercongruence term, vce(cluster term)
eststo cq1

logit win ib4.gender_dyad##c.term ib4.gender_dyad##c.scotuslowercongruence ib3.lawyerstat_p c.movingwall_diff i.appointed_p c.amici_diff ib4.support_pf i.previoussg_p i.clerk_p i.cqSalience i.legalSalience , vce(cluster term)
eststo cq2

logit win ib4.gender_dyad ib3.lawyerstat_p c.movingwall_diff i.appointed_p c.amici_diff ib4.support_pf i.previoussg_p i.clerk_p salienceest i.legalSalience scotuslowercongruence term, vce(cluster term)
eststo clark1

logit win ib4.gender_dyad##c.term ib4.gender_dyad##c.scotuslowercongruence ib3.lawyerstat_p c.movingwall_diff i.appointed_p c.amici_diff ib4.support_pf i.previoussg_p i.clerk_p salienceest i.legalSalience , vce(cluster term)
eststo clark2

esttab nyt1  nyt2 cq1 cq2 clark1 clark2 using saliencelogitCoefs.rtf, replace title("Table A4. Comparing Measures of Case Salience") mtitle("NYTimes Model 1" "NYTimes Model 2" "CQ Model 1" "CQ Model 2" "Clark Model 1" "Clark Model 2") nodepvars nonumbers nogap nobaselevels label varwidth(25)  cells((b(star fmt(2))) se(par)) interaction(" x ") addnotes("Note: Logit coefficients clustered by Supreme Court Term with standard errors in parentheses. *p < 0.05")

*table a.5 - issues area
logit win ib4.gender_dyad##i.issueArea ib3.lawyerstat_p c.movingwall_diff i.appointed_p c.amici_diff ib4.support_pf i.previoussg_p i.clerk_p CSI i.legalSalience scotuslowercongruence term, vce( cluster term)
eststo issueArea
esttab issueArea using issAreaLogitCoefs.rtf, replace title("Table A5. Controlling for Issue Area") mtitle("Coefficients") nodepvars nonumbers nogap nobaselevels label varwidth(25)  cells((b(star fmt(2))) se(par)) interaction(" x ") addnotes("Note: Logit coefficients clustered by Supreme Court Term with standard errors in parentheses. *p < 0.05")

*table a.6 - law clerks
logit win ib4.gender_dyad##i.clerk_p ib3.lawyerstat_p c.movingwall_diff i.appointed_p c.amici_diff ib4.support_pf i.previoussg_p CSI i.legalSalience scotuslowercongruence  term, vce(cluster term)
eststo lawclerk
esttab lawclerk using lawClerkLogitCoefs.rtf, replace title("Table A6. Controlling for Law Clerk Status") mtitle("Coefficients") nodepvars nonumbers nogap nobaselevels label varwidth(25)  cells((b(star fmt(2))) se(par)) interaction(" x ") addnotes("Note: Logit coefficients clustered by Supreme Court Term with standard errors in parentheses. *p < 0.05")



***Justice level models
*change frame
frame change JL

*creating dv
gen petVotes = 1 if partyWinning == 1 & majority == 2
replace petVotes = 1 if partyWinning == 0 & majority == 1
replace petVotes = 0 if partyWinning == 1 & majority == 1
replace petVotes = 0 if partyWinning == 0 & majority == 2

*table a.8 - justice issues
logit petVotes ib4.gender_dyad##c.justicelowercongruence ib3.lawyerstat_p##c.justicelowercongruence c.movingwall_diff i.appointed_p c.amici_diff i.acSupport_p i.previoussg_p CSI i.legalSalience  term, vce(cluster term)
margins, dydx(*)
margins ib4.gender_dyad, at(justicelowercongruence = (-7.89(1)7.89)) contrast(nowald effects)
marginsplot, recast(line) recastci(rarea) yline(0, lpattern(-)) by(gender_dyad) ytitle("Pr(Petitioner Wins)")


eststo justModel
esttab justModel using justicesLogitCoefs.rtf, replace title("Table A8. Justice-level model testing influences on voting for Petitioner Success") mtitle("Coefficients") nodepvars nonumbers nogap nobaselevels label varwidth(25)  cells((b(star fmt(2))) se(par)) interaction(" x ") addnotes("Note: Logit coefficients clustered by Supreme Court Term with standard errors in parentheses. *p < 0.05")

***Vote Count models
frame change CL
*creating dv
gen votesForPet = majVote if partyWinning==1
replace votesForPet = minVote if partyWinning==0

*OLS
reg votesForPet ib4.gender_dyad ib3.lawyerstat_p c.movingwall_diff i.appointed_p c.amici_diff ib4.support_pf i.previoussg_p i.clerk_p CSI i.legalSalience term, vce( cluster term)
eststo votesOLS

*Neg Binomial
nbreg votesForPet ib4.gender_dyad ib3.lawyerstat_p c.movingwall_diff i.appointed_p c.amici_diff ib4.support_pf i.previoussg_p i.clerk_p CSI i.legalSalience term, vce( cluster term)
eststo votesNBREG

esttab votesOLS votesNBREG using voteSpreadCoefs.rtf, replace title("Table A9. Votes in-Favor of Petitioner") mtitle("OLS" "Negative Binomial") nodepvars nonumbers nogap nobaselevels label varwidth(25)  cells((b(star fmt(2))) se(par)) interaction(" x ") addnotes("Note: Logit coefficients clustered by Supreme Court Term with standard errors in parentheses. *p < 0.05")


